package Hot100;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class findAnagrams {
    public static void main(String[] args) {
        String s = "abab";
        String p = "ab";
        List list = findAnagrams(s,p);
        System.out.println(list);
    }
    public static List<Integer> findAnagrams(String s, String p) {
        LinkedList<Integer> list = new LinkedList();
        int[] num1 = new int[26];
        int[] temp = new int[26];
        char[] sarrays = s.toCharArray();
        char[] arrays = p.toCharArray();
        for (int i = 0; i < arrays.length; i++) {
            num1[((int) arrays[i]) - 97]++;
        }

        int x = 0;
        boolean psp = false;
        for (int i = 0; i <= sarrays.length - p.length(); i++) {
            x = 0;
            temp = Arrays.copyOf(num1,num1.length);
            psp = false;
            while(x < p.length()){
                temp[(int) sarrays[i + x] - 97] --;
                if(temp[(int) sarrays[i + x] - 97] < 0){
                    psp = true;
                    break;
                }
                x++;
            }
            if(psp == false){
                list.add(i);
            }

        }
        return list;
    }
}
